using System;
using Habanero.Base;
using Habanero.BO;
using Habanero.UI.Base;
using Habanero.UI.Win;
using UseCaseManager.BO;
using UseCaseManager.Logic;

namespace UseCaseManager.UI
{
    /// <summary>
    /// A parent-child control with a Grid Selector for The Drops and a MultiSelector for Selecting the Use cases in the Drop.
    /// </summary>
    public class DropPlanningControl : UserControlWin, IFormControl
    {
        private readonly IControlFactory _controlFactory;
        private BusinessObjectCollection<Drop> _dropCol;
        /// <summary>
        /// The Selector used to Select the Drop that the Use cases are being selected for.
        /// </summary>
        public DropsGridControl DropSelector { private set; get; }
        /// <summary>
        /// A multi selector showing the Use cases selected for the drop and the Use Cases available for selection.
        /// </summary>
        public UseCasesSelectorControl UseCaseSelectorCntrl { get; private set; }
        public ButtonGroupControlWin _buttonGroupControlWin;
        /// <summary>
        /// Constructor for the <see cref="DropPlanningControl"/>
        /// </summary>
        public DropPlanningControl()
        {
            _controlFactory = GlobalUIRegistry.ControlFactory;
            DropSelector = new DropsGridControl(new ControlFactoryWin());
            UseCaseSelectorCntrl = new UseCasesSelectorControl();
            _buttonGroupControlWin = new ButtonGroupControlWin(GlobalUIRegistry.ControlFactory);
            ExportDropsButton = _buttonGroupControlWin.AddButton("ExportDrops", @"Export &Drops", ClickHandlerExportDrops);
            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(this);
            DropSelector.Width = 350;
            layoutManager.AddControl(DropSelector, BorderLayoutManager.Position.West);
            layoutManager.AddControl(UseCaseSelectorCntrl, BorderLayoutManager.Position.Centre, true);
            layoutManager.AddControl(_buttonGroupControlWin, BorderLayoutManager.Position.South);

            // Connect the Drop and UseCases MultiSelector so that when an item is selected
            // in the Drops grid, the correct UseCases are shown in the child MultiSelector.
            DropSelector.GridSelector.Grid.BusinessObjectSelected +=
                delegate
                {
                    try
                    {
                        Drop selectedDrop = DropSelector.GridSelector.Grid.SelectedBusinessObject as Drop;
                        if (selectedDrop == null) return;
                        UseCaseSelectorCntrl.DropBeingAssigned = selectedDrop;
                        UseCaseSelectorCntrl.Release = Release;
                    }
                    catch (Exception ex)
                    {
                        GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
                    }
                };
        }

        /// <summary>
        /// Returns the ExportDropsButton that is used to Export Drop Information for all the drops in the release.
        /// </summary>
        public IButton ExportDropsButton { get; private set; }
        //TODO: implement global registry to to get project
        private void ClickHandlerExportDrops(object sender, EventArgs e)
        {
            _project = Broker.GetBusinessObjectCollection<Project>("", "")[0];
           if (_project != null) CreateAndExportTable.ExportToCSV(_project);
           else
           {
               GlobalUIRegistry.ControlFactory.ShowMessageBox("No Drops to export.", "Export Drops",
                                                           MessageBoxButtons.OK, MessageBoxIcon.None);
           }
        }

        //needs tests
        private Project _project;

        //needs tests
        public Release Release { get; set; }

        /// <summary>
        /// Returns the collection of drops that is being shown in the <see cref="DropSelector"/>
        /// </summary>
        public BusinessObjectCollection<Drop> DropCol
        {
            get { return _dropCol; }
            set
            {
                _dropCol = value;
                DropSelector.GridSelector.BusinessObjectCollection = _dropCol;
            }
        }

        /// <summary>
        /// Called whenever the control is placed on a form by the menu system.  Put any initialisation
        /// code in here that must be run whenever the form is displayed.
        /// </summary>
        public void SetForm(IFormHabanero form)
        {
        }
    }
}